package com.zhangxiaofanfan.hotcode.first;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * <a href="https://leetcode.cn/problems/permutations/description/?envType=study-plan-v2&envId=top-100-liked">46. 全排列</a>
 *
 * @author zhangxiaofanfan
 * @date 2024-06-26 20:33:33
 */
public class HotCode055 {
    public static void main(String[] args) {

    }

    List<List<Integer>> result = new ArrayList<>();
    public List<List<Integer>> permute(int[] nums) {
        Set<Integer> residue = new HashSet<>();
        for (int i = 0; i < nums.length; i++) {
            residue.add(i);
        }
        bfs(nums, residue, new ArrayList<>());
        return result;
    }

    public void bfs(int[] nums, Set<Integer> residue, List<Integer> list) {
        if (residue.isEmpty()) {
            result.add(new ArrayList<>(list));
        }
        HashSet<Integer> temp = new HashSet<>(residue);
        residue.forEach(key -> {
            list.add(nums[key]);
            temp.remove(key);
            bfs(nums, temp, list);
            temp.add(key);
            list.removeLast();
        });
    }
}
